import {message} from 'antd';
import { queryService, queryCategories, fetchService, addService, changeService, removeService } from '../services/service';

export default {
  namespace: 'service',

  state: {
      data: {list: [], pagination: {size: 10, current: 1, total: 0}},
      categories: [],
      item: {},
      loading: false,
      maskLoading: false,
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryService, payload);
      yield put({
        type: 'save',
        payload: Array.isArray(response.data) ? response : [],
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
      *fetchCategories({_}, { call, put }) {
          const response = yield call(queryCategories, {pid: 0});
          yield put({
              type: 'saveCategory',
              payload: Array.isArray(response.data) ? response : [],
          });
      },
      *fetchService({payload, callback}, {call, put}) {
          yield put({
              type: 'changeMaskLoading',
              payload: true,
          });
          const response = yield call(fetchService, payload.id);
          if (response.status) {
              const data = response.data;
              if (data.thumb) {
                  data.thumbFile = data.thumb.replace('https://zzbj.kooki.cn/', '');
              }
              if (data.photos && data.photos.length > 0) {
                  data.photosFile = data.photos;
              }
              yield put({
                  type: 'saveServiceData',
                  payload: data,
              });
              if (callback) {
                  callback(response.data);
              }
          }
          yield put({
              type: 'changeMaskLoading',
              payload: false,
          });
      },
      *edit({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(changeService, payload);
          if (response.status) {
              message.success('编辑成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *add({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(addService, payload);
          if (response.status) {
              message.success('添加成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *change({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(changeService, payload);
          if (response.status) {
              message.success('修改成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *remove({payload, callback}, {call, put}) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(removeService, payload);
          if (response.status) {
              message.success('删除成功');
              if (callback) {
                  callback();
              }
          } else {
              message.error(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
  },

  reducers: {
      save(state, action) {
      return {
        ...state,
        data: {list: action.payload.data, pagination: {pageSize: action.payload.page.size, total: action.payload.page.total, current: action.payload.page.current}},
      };
    },
      saveCategory(state, action) {
          return {
              ...state,
              categories: action.payload.data,
          };
      },
      saveServiceData(state, action) {
          return {
              ...state,
              item: action.payload,
          };
      },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
      changeMaskLoading(state, action) {
          return {
              ...state,
              maskLoading: action.payload,
          };
      },
},
};
